View Javadoc

1   /*
2    * jGuild Project: jRPM
3    * Released under the Apache License ( http://www.apache.org/LICENSE )
4    */
5   package com.jguild.jrpm.io;
6   
7   import com.jguild.jrpm.io.constant.LeadArchitecture;
8   import com.jguild.jrpm.io.constant.LeadOS;
9   import com.jguild.jrpm.io.constant.LeadSignature;
10  import com.jguild.jrpm.io.constant.LeadType;
11  import com.jguild.jrpm.io.datatype.*;
12  
13  import org.apache.log4j.Logger;
14  
15  import java.io.DataInputStream;
16  import java.io.IOException;
17  
18  
19  /***
20   * The Java Version of the C struct for the lead.
21   *
22   * <code><pre>C struct :
23   * struct rpmlead {
24   *    unsigned char magic[4];
25   *    unsigned char major, minor;
26   *    short type;            (2 byte)
27   *    short archnum;         (2 byte)
28   *    char name[66];
29   *    short osnum;           (2 byte)
30   *    short signature_type;  (2 byte)
31   *    char reserved[16];
32   * } ;</pre></code>
33   *
34   * @version $Id: RPMLead.java,v 1.6 2003/10/20 16:32:11 mkuss Exp $
35   */
36  public class RPMLead {
37      private static final Logger logger = Logger.getLogger(RPMLead.class);
38      private LeadArchitecture arch;
39      private LeadOS os;
40      private LeadSignature sigType;
41      private LeadType type;
42      private String name;
43      private int major;
44      private int minor;
45  
46      protected RPMLead(DataInputStream inputStream)
47          throws IOException {
48          if (logger.isDebugEnabled()) {
49              logger.debug("Start Reading Lead");
50          }
51  
52          try {
53              check(inputStream.readUnsignedByte() == 0xED);
54              check(inputStream.readUnsignedByte() == 0xAB);
55              check(inputStream.readUnsignedByte() == 0xEE);
56              check(inputStream.readUnsignedByte() == 0xDB);
57              major = inputStream.readUnsignedByte();
58  
59              if (logger.isDebugEnabled()) {
60                  logger.debug("major: " + major);
61              }
62  
63              minor = inputStream.readUnsignedByte();
64  
65              if (logger.isDebugEnabled()) {
66                  logger.debug("minor: " + minor);
67              }
68  
69              type = LeadType.getLeadType(inputStream.readUnsignedShort());
70  
71              if (logger.isDebugEnabled()) {
72                  logger.debug("type: " + type.getName());
73              }
74  
75              arch = LeadArchitecture.getLeadArchitecture(inputStream.readUnsignedShort());
76  
77              if (logger.isDebugEnabled()) {
78                  logger.debug("arch: " + arch.getName());
79              }
80  
81              byte[] nameBuf = new byte[66];
82  
83              inputStream.readFully(nameBuf);
84              name = RPMUtil.cArrayToString(nameBuf, 0);
85  
86              if (logger.isDebugEnabled()) {
87                  logger.debug("name: " + name);
88              }
89  
90              os = LeadOS.getLeadOS(inputStream.readUnsignedShort());
91  
92              if (logger.isDebugEnabled()) {
93                  logger.debug("os: " + os.getName());
94              }
95  
96              sigType = LeadSignature.getLeadSignature(inputStream.readUnsignedShort());
97  
98              if (logger.isDebugEnabled()) {
99                  logger.debug("sigType: " + sigType.getName());
100             }
101 
102             inputStream.skipBytes(16);
103 
104             check(major < 5);
105             check(!type.equals(LeadType.UNKNOWN));
106 
107             // Ignore os and arch for source rpms
108             if (!type.equals(LeadType.SOURCE)) {
109                 check(!os.equals(LeadOS.UNKNOWN));
110                 check(!arch.equals(LeadArchitecture.UNKNOWN));
111             }
112 
113             check(!sigType.equals(LeadType.UNKNOWN));
114         } finally {
115             if (logger.isDebugEnabled()) {
116                 logger.debug("Finished Reading Lead");
117             }
118         }
119     }
120 
121     /***
122      * Get the architecture of a rpm
123      *
124      * @return the architecture
125      */
126     public LeadArchitecture getArchitecture() {
127         return arch;
128     }
129 
130     /***
131      * Get the major number of the rpm version a rpm is build for
132      *
133      * @return The major number
134      */
135     public int getMajorVersion() {
136         return major;
137     }
138 
139     /***
140      * Get the minor number of the rpm version a rpm is build for
141      *
142      * @return The minor number
143      */
144     public int getMinorVersion() {
145         return minor;
146     }
147 
148     /***
149      * Get the name of this rpm. This field has a maximum length of 65
150      * characters + one for the ending \0 for c strings.
151      *
152      * @return The name
153      */
154     public String getName() {
155         return name;
156     }
157 
158     /***
159      * Get the operation system a rpm is build for
160      *
161      * @return The operation system
162      */
163     public LeadOS getOperationSystem() {
164         return os;
165     }
166 
167     /***
168      * Get the type of signature this rpm provides
169      *
170      * @return The signature
171      */
172     public LeadSignature getSignatureType() {
173         return sigType;
174     }
175 
176     /***
177      * Get the size of this structure in bytes (as defined for a rpm file)
178      *
179      * @return The size (96 bytes)
180      */
181     public final long getSize() {
182         return 96;
183     }
184 
185     /***
186      * Get the type of a rpm file (binary or source)
187      *
188      * @return The type
189      */
190     public LeadType getType() {
191         return type;
192     }
193 
194     private static final void check(boolean test)
195         throws IOException {
196         if (!test) {
197             throw new IOException("Corrupted archive");
198         }
199     }
200 }